home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / GNU_KIT / DISK8.ZIP / src / makest / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-08  |  12.2 KB  |  563 lines

  1. /* Copyright (C) 1988-1991 Free Software Foundation, Inc.
  2. This file is part of GNU Make.
  3.  
  4. GNU Make is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8.  
  9. GNU Make is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Make; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "make.h"
  19. #include "dep.h"
  20.  
  21.  
  22. /* Compare strings *S1 and *S2.
  23.    Return negative if the first is less, positive if it is greater,
  24.    zero if they are equal.  */
  25.  
  26. int
  27. alpha_compare (s1, s2)
  28.      char **s1, **s2;
  29. {
  30.   if (**s1 != **s2)
  31.     return **s1 - **s2;
  32.   return strcmp (*s1, *s2);
  33. }
  34.  
  35. /* Discard each backslash-newline combination from LINE.
  36.    Backslash-backslash-newline combinations become backslash-newlines.
  37.    This is done by copying the text at LINE into itself.  */
  38.  
  39. void
  40. collapse_continuations (line)
  41.      char *line;
  42. {
  43.   register char *in, *out, *p;
  44.   register int backslash;
  45.   register unsigned int bs_write;
  46.  
  47.   in = index (line, '\n');
  48.   if (in == 0)
  49.     return;
  50.  
  51.   out = in;
  52.   if (out > line)
  53.     while (out[-1] == '\\')
  54.       --out;
  55.  
  56.   while (*in != '\0')
  57.     {
  58.       /* BS_WRITE gets the number of quoted backslashes at
  59.      the end just before IN, and BACKSLASH gets nonzero
  60.      if the next character is quoted.  */
  61.       backslash = 0;
  62.       bs_write = 0;
  63.       for (p = in - 1; p >= line && *p == '\\'; --p)
  64.     {
  65.       if (backslash)
  66.         ++bs_write;
  67.       backslash = !backslash;
  68.  
  69.       /* It should be impossible to go back this far without exiting,
  70.          but if we do, we can't get the right answer.  */
  71.       if (in == out - 1)
  72.         abort ();
  73.     }
  74.  
  75.       /* Output the appropriate number of backslashes.  */
  76.       while (bs_write-- > 0)
  77.     *out++ = '\\';
  78.  
  79.       /* Skip the newline.  */
  80.       ++in;
  81.  
  82.       /* If the newline is quoted, discard following whitespace
  83.      and any preceding whitespace; leave just one space.  */
  84.       if (backslash)
  85.     {
  86.       in = next_token (in);
  87.       while (out > line && (out[-1] == ' ' || out[-1] == '\t'))
  88.         --out;
  89.       *out++ = ' ';
  90.     }
  91.       else
  92.     /* If the newline isn't quoted, put it in the output.  */
  93.     *out++ = '\n';
  94.  
  95.       /* Now copy the following line to the output.
  96.      Stop when we find backslashes followed by a newline.  */
  97.       while (*in != '\0')
  98.     if (*in == '\\')
  99.       {
  100.         p = in + 1;
  101.         while (*p == '\\')
  102.           ++p;
  103.         if (*p == '\n')
  104.           {
  105.         in = p;
  106.         break;
  107.           }
  108.         while (in < p)
  109.           *out++ = *in++;
  110.       }
  111.     else
  112.       *out++ = *in++;
  113.     }
  114.  
  115.   *out = '\0';
  116. }
  117.  
  118.  
  119. /* Remove comments from LINE.
  120.    This is done by copying the text at LINE onto itself.  */
  121.  
  122. void
  123. remove_comments (line)
  124.      char *line;
  125. {
  126.   register char *p, *p2;
  127.   register int backslash;
  128.   register unsigned int bs_write;
  129.  
  130.   while (1)
  131.     {
  132.       p = index (line, '#');
  133.       if (p == 0)
  134.     break;
  135.  
  136.       backslash = 0;
  137.       bs_write = 0;
  138.       for (p2 = p - 1; p2 > line && *p2 == '\\'; --p2)
  139.     {
  140.       if (backslash)
  141.         ++bs_write;
  142.       backslash = !backslash;
  143.     }
  144.  
  145.       if (!backslash)
  146.     {
  147.       /* Cut off the line at the #.  */
  148.       *p = '\0';
  149.       break;
  150.     }
  151.  
  152.       /* strcpy better copy left to right.  */
  153.       line = p;
  154.       strcpy (p2 + 1 + bs_write, line);
  155.     }
  156. }
  157.  
  158. /* Print N spaces (used by DEBUGPR for target-depth).  */
  159.  
  160. void
  161. print_spaces (n)
  162.      register unsigned int n;
  163. {
  164.   while (n-- > 0)
  165.     putchar (' ');
  166. }
  167.  
  168.  
  169. /* Return a newly-allocated string whose contents
  170.    concatenate those of s1, s2, s3.  */
  171.  
  172. char *
  173. concat (s1, s2, s3)
  174.      register char *s1, *s2, *s3;
  175. {
  176.   register unsigned int len1, len2, len3;
  177.   register char *result;
  178.  
  179.   len1 = *s1 != '\0' ? strlen (s1) : 0;
  180.   len2 = *s2 != '\0' ? strlen (s2) : 0;
  181.   len3 = *s3 != '\0' ? strlen (s3) : 0;
  182.  
  183.   result = (char *) xmalloc (len1 + len2 + len3 + 1);
  184.  
  185.   if (*s1 != '\0')
  186.     bcopy (s1, result, len1);
  187.   if (*s2 != '\0')
  188.     bcopy (s2, result + len1, len2);
  189.   if (*s3 != '\0')
  190.     bcopy (s3, result + len1 + len2, len3);
  191.   *(result + len1 + len2 + len3) = '\0';
  192.  
  193.   return result;
  194. }
  195.  
  196. /* Print a message on stdout.  */
  197.  
  198. void
  199. message (s1, s2, s3, s4, s5, s6)
  200.      char *s1, *s2, *s3, *s4, *s5, *s6;
  201. {
  202.   if (makelevel == 0)
  203.     printf ("%s: ", program);
  204.   else
  205.     printf ("%s[%u]: ", program, makelevel);
  206.   printf (s1, s2, s3, s4, s5, s6);
  207.   putchar ('\n');
  208.   fflush (stdout);
  209. }
  210.  
  211. /* Print an error message and exit.  */
  212.  
  213. /* VARARGS1 */
  214. void
  215. fatal (s1, s2, s3, s4, s5, s6)
  216.      char *s1, *s2, *s3, *s4, *s5, *s6;
  217. {
  218.   if (makelevel == 0)
  219.     fprintf (stderr, "%s: ", program);
  220.   else
  221.     fprintf (stderr, "%s[%u]: ", program, makelevel);
  222.   fprintf (stderr, s1, s2, s3, s4, s5, s6);
  223.   fputs (".  Stop.\n", stderr);
  224.  
  225.   die (1);
  226. }
  227.  
  228. /* Print error message.  `s1' is printf control string, `s2' is arg for it. */
  229.  
  230. /* VARARGS1 */
  231.  
  232. void
  233. error (s1, s2, s3, s4, s5, s6)
  234.      char *s1, *s2, *s3, *s4, *s5, *s6;
  235. {
  236.   if (makelevel == 0)
  237.     fprintf (stderr, "%s: ", program);
  238.   else
  239.     fprintf (stderr, "%s[%u]: ", program, makelevel);
  240.   fprintf (stderr, s1, s2, s3, s4, s5, s6);
  241.   putc ('\n', stderr);
  242.   fflush (stderr);
  243. }
  244.  
  245. /* Print an error message from errno.  */
  246.  
  247. void
  248. perror_with_name (str, name)
  249.      char *str, *name;
  250. {
  251.   extern int errno, sys_nerr;
  252.   extern char *sys_errlist[];
  253.  
  254.   if (errno < sys_nerr)
  255.     error ("%s%s: %s", str, name, sys_errlist[errno]);
  256.   else
  257.     error ("%s%s: Unknown error %d", str, name, errno);
  258. }
  259.  
  260. /* Print an error message from errno and exit.  */
  261.  
  262. void
  263. pfatal_with_name (name)
  264.      char *name;
  265. {
  266.   extern int errno, sys_nerr;
  267.   extern char *sys_errlist[];
  268.  
  269.   if (errno < sys_nerr)
  270.     fatal ("%s: %s", name, sys_errlist[errno]);
  271.   else
  272.     fatal ("%s: Unknown error %d", name, errno);
  273.  
  274.   /* NOTREACHED */
  275. }
  276.  
  277. /* Like malloc but get fatal error if memory is exhausted.  */
  278.  
  279. #undef xmalloc
  280. #undef xrealloc
  281.  
  282. char *
  283. xmalloc (size)
  284.      unsigned int size;
  285. {
  286.   char *result = malloc (size);
  287.   if (result == 0)
  288.     fatal ("virtual memory exhausted");
  289.   return result;
  290. }
  291.  
  292.  
  293. char *
  294. xrealloc (ptr, size)
  295.      char *ptr;
  296.      unsigned int size;
  297. {
  298.   char *result = realloc (ptr, size);
  299.   if (result == 0)
  300.     fatal ("virtual memory exhausted");
  301.   return result;
  302. }
  303.  
  304. char *
  305. savestring (str, length)
  306.      char *str;
  307.      unsigned int length;
  308. {
  309.   register char *out = (char *) xmalloc (length + 1);
  310.   if (length > 0)
  311.     bcopy (str, out, length);
  312.   out[length] = '\0';
  313.   return out;
  314. }
  315.  
  316. /* Search string BIG (length BLEN) for an occurrence of
  317.    string SMALL (length SLEN).  Return a pointer to the
  318.    beginning of the first occurrence, or return nil if none found.  */
  319.  
  320. char *
  321. sindex (big, blen, small, slen)
  322.      char *big;
  323.      unsigned int blen;
  324.      char *small;
  325.      unsigned int slen;
  326. {
  327.   register unsigned int b;
  328.  
  329.   if (blen < 1)
  330.     blen = strlen (big);
  331.   if (slen < 1)
  332.     slen = strlen (small);
  333.  
  334.   for (b = 0; b < blen; ++b)
  335.     if (big[b] == *small && !strncmp (&big[b + 1], small + 1, slen - 1))
  336.       return (&big[b]);
  337.  
  338.   return 0;
  339. }
  340.  
  341. /* Limited INDEX:
  342.    Search through the string STRING, which ends at LIMIT, for the character C.
  343.    Returns a pointer to the first occurrence, or nil if none is found.
  344.    Like INDEX except that the string searched ends where specified
  345.    instead of at the first null.  */
  346.  
  347. char *
  348. lindex (s, limit, c)
  349.      register char *s, *limit;
  350.      int c;
  351. {
  352.   while (s < limit)
  353.     if (*s++ == c)
  354.       return s - 1;
  355.  
  356.   return 0;
  357. }
  358.  
  359. /* Return the address of the first whitespace or null in the string S.  */
  360.  
  361. char *
  362. end_of_token (s)
  363.      char *s;
  364. {
  365.   register char *p = s;
  366.   register int backslash = 0;
  367.  
  368.   while (*p != '\0' && (backslash || (*p != ' ' && *p != '\t' && *p != '\f')))
  369.     {
  370.       if (*p++ == '\\')
  371.     {
  372.       backslash = !backslash;
  373.       while (*p == '\\')
  374.         {
  375.           backslash = !backslash;
  376.           ++p;
  377.         }
  378.     }
  379.       else
  380.     backslash = 0;
  381.     }
  382.  
  383.   return p;
  384. }
  385.  
  386. /* Return the address of the first nonwhitespace or null in the string S.  */
  387.  
  388. char *
  389. next_token (s)
  390.      char *s;
  391. {
  392.   register char *p = s;
  393.  
  394.   while (*p == ' ' || *p == '\t' || *p == '\f')
  395.     ++p;
  396.   return p;
  397. }
  398.  
  399. /* Find the next token in PTR; return the address of it, and store the
  400.    length of the token into *LENGTHPTR if LENGTHPTR is not nil.  */
  401.  
  402. char *
  403. find_next_token (ptr, lengthptr)
  404.      char **ptr;
  405.      unsigned int *lengthptr;
  406. {
  407.   char *p = next_token (*ptr);
  408.   char *end;
  409.  
  410.   if (*p == '\0')
  411.     return 0;
  412.  
  413.   *ptr = end = end_of_token (p);
  414.   if (lengthptr != 0)
  415.     *lengthptr = end - p;
  416.   return p;
  417. }
  418.  
  419. /* Copy a chain of `struct dep', making a new chain
  420.    with the same contents as the old one.  */
  421.  
  422. struct dep *
  423. copy_dep_chain (d)
  424.      register struct dep *d;
  425. {
  426.   register struct dep *c;
  427.   struct dep *firstnew = 0;
  428.   struct dep *lastnew;
  429.  
  430.   while (d != 0)
  431.     {
  432.       c = (struct dep *) xmalloc (sizeof (struct dep));
  433.       bcopy ((char *) d, (char *) c, sizeof (struct dep));
  434.       if (c->name != 0)
  435.     c->name = savestring (c->name, strlen (c->name));
  436.       c->next = 0;
  437.       if (firstnew == 0)
  438.     firstnew = lastnew = c;
  439.       else
  440.     lastnew = lastnew->next = c;
  441.  
  442.       d = d->next;
  443.     }
  444.  
  445.   return firstnew;
  446. }
  447.  
  448. #ifdef    iAPX286
  449. /* The losing compiler on this machine can't handle this macro.  */
  450.  
  451. char *
  452. dep_name (dep)
  453.      struct dep *dep;
  454. {
  455.   return dep->name == 0 ? dep->file->name : dep->name;
  456. }
  457. #endif
  458.  
  459. /* Keep track of the user and group IDs for user- and make- access.  */
  460. static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
  461. #define    access_inited    (user_uid != -1)
  462. static enum { make, user } current_access;
  463.  
  464. static void
  465. init_access ()
  466. {
  467. #ifndef atarist
  468.   user_uid = getuid ();
  469.   user_gid = getgid ();
  470.  
  471.   make_uid = geteuid ();
  472.   make_gid = getegid ();
  473.  
  474.   /* Do these ever fail?  */
  475.   if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
  476.     pfatal_with_name ("get{e}[gu]id");
  477. #endif /* atarist */
  478.  
  479.   current_access = make;
  480. }
  481.  
  482. /* Give the process appropriate permissions for access to
  483.    user data (i.e., to stat files, or to spawn a child process).  */
  484. #ifdef atarist
  485. void
  486. user_access ()
  487. {
  488.   current_access = user;
  489. }
  490. #else
  491. void
  492. user_access ()
  493. {
  494.   if (!access_inited)
  495.     init_access ();
  496.  
  497.   if (current_access == user)
  498.     return;
  499.  
  500.   /* We are in "make access" mode.  This means that the effective user and
  501.      group IDs are those of make (if it was installed setuid or setgid).
  502.      We now want to set the effective user and group IDs to the real IDs,
  503.      which are the IDs of the process that exec'd make.  */
  504.  
  505. #ifdef    USG
  506.   /* System V has only the setuid/setgid calls to set user/group IDs.
  507.      There is an effective ID, which can be set by setuid/setgid.
  508.      It can be set (unless you are root) only to either what it already is
  509.      (returned by geteuid/getegid, now in make_uid/make_gid),
  510.      the real ID (return by getuid/getgid, now in user_uid/user_gid),
  511.      or the saved set ID (what the effective ID was before this set-ID
  512.      executable (make) was exec'd).  */
  513.   if (setuid (user_uid) < 0)
  514.     pfatal_with_name ("setuid");
  515.   if (setgid (user_gid) < 0)
  516.     pfatal_with_name ("setgid");
  517. #else
  518.   /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
  519.      They may be set to themselves or each other.  So you have two alternatives
  520.      at any one time.  If you use setuid/setgid, the effective will be set to
  521.      the real, leaving only one alternative.  Using setreuid/setregid, however,
  522.      you can toggle between your two alternatives by swapping the values in a
  523.      single setreuid or setregid call.  */
  524.   if (setreuid (make_uid, user_uid) < 0)
  525.     pfatal_with_name ("setreuid");
  526.   if (setregid (make_gid, user_gid) < 0)
  527.     pfatal_with_name ("setregid");
  528. #endif
  529.  
  530.   current_access = user;
  531. }
  532. #endif /* atarist */
  533.  
  534. /* Give the process appropriate permissions for access to
  535.    make data (i.e., the load average).  */
  536. void
  537. make_access ()
  538. {
  539. #ifndef atarist
  540.   if (!access_inited)
  541.     init_access ();
  542.  
  543.   if (current_access == make)
  544.     return;
  545.  
  546.   /* See comments in user_access, above.  */
  547.  
  548. #ifdef    USG
  549.   if (setuid (make_uid) < 0)
  550.     pfatal_with_name ("setuid");
  551.   if (setgid (make_gid) < 0)
  552.     pfatal_with_name ("setgid");
  553. #else
  554.   if (setreuid (user_uid, make_uid) < 0)
  555.     pfatal_with_name ("setreuid");
  556.   if (setregid (user_gid, make_gid) < 0)
  557.     pfatal_with_name ("setregid");
  558. #endif
  559. #endif /* atarist */
  560.  
  561.   current_access = make;
  562. }
  563.